package com.yu.service.system;

import java.util.Collections;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.yu.dto.sys.MenuTreeDTO;
import com.yu.dto.sys.MetaDTO;
import com.yu.entity.system.MenuConditionEntity;
import com.yu.entity.system.MenuEntity;
import com.yu.mapper.system.MenuMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yu.entity.ResponsePageEntity;

/**
 * 菜单 服务层
 *
 * @author maxinyu 该代码由maxinyu生成
 * @date 2025-06-02 15:26:06
 */
@Service
public class MenuService {

	@Autowired
	private MenuMapper menuMapper;

	/**
     * 查询菜单信息
     *
     * @param id 菜单ID
     * @return 菜单信息
     */
	public MenuEntity findById(Long id) {
	    return menuMapper.findById(id);
	}

	/**
     * 根据条件分页查询菜单列表
     *
     * @param menuConditionEntity 菜单信息
     * @return 菜单集合
     */
	public ResponsePageEntity<MenuEntity> searchByPage(MenuConditionEntity menuConditionEntity) {
		int count = menuMapper.searchCount(menuConditionEntity);
		if (count == 0) {
			return ResponsePageEntity.buildEmpty(menuConditionEntity);
		}
		List<MenuEntity> dataList = menuMapper.searchByCondition(menuConditionEntity);
		return ResponsePageEntity.build(menuConditionEntity, count, dataList);
	}

    /**
     * 新增菜单
     *
     * @param menuEntity 菜单信息
     * @return 结果
     */
	public int insert(MenuEntity menuEntity) {
	    return menuMapper.insert(menuEntity);
	}

	/**
     * 修改菜单
     *
     * @param menuEntity 菜单信息
     * @return 结果
     */
	public int update(MenuEntity menuEntity) {
	    return menuMapper.update(menuEntity);
	}

	/**
	 * 删除菜单对象
	 *
	 * @param id 系统ID
	 * @return 结果
	 */
	public int deleteById(Long id) {
		return menuMapper.deleteById(id);
	}

	public List<MenuTreeDTO> getMenuTree(){
		MenuConditionEntity menuConditionEntity=new MenuConditionEntity();
		menuConditionEntity.setPageSize(0);
		menuConditionEntity.setPid(0L);
		List<MenuEntity> menuEntities = menuMapper.searchByCondition(menuConditionEntity);
		if(CollectionUtils.isEmpty(menuEntities)){
			return Collections.emptyList();
		}

		List<MenuTreeDTO> result = Lists.newArrayList();
		for(MenuEntity menuEntity:menuEntities){
			MenuTreeDTO menuTreeDTO=buildMenuTreeDTO(menuEntity);
			menuTreeDTO.setAlwaysShow(true);
			result.add(menuTreeDTO);
			buildChildren(menuEntity,menuTreeDTO);
		}
		return result;
	}

	private void buildChildren(MenuEntity menuEntity,MenuTreeDTO menuTreeDTO){
		MenuConditionEntity menuConditionEntity=new MenuConditionEntity();
		menuConditionEntity.setPageSize(0);
		menuConditionEntity.setPid(menuEntity.getId());
		List<MenuEntity>childrenEntities=menuMapper.searchByCondition(menuConditionEntity);
		if(CollectionUtils.isNotEmpty(childrenEntities)){
			for(MenuEntity childrenEntity:childrenEntities){
				MenuTreeDTO childrenMenuTreeDTO=buildMenuTreeDTO(childrenEntity);
				menuTreeDTO.addChildren(childrenMenuTreeDTO);
				buildChildren(childrenEntity,childrenMenuTreeDTO);
			}
		}
	}


	private MenuTreeDTO buildMenuTreeDTO(MenuEntity menuEntity){
		MenuTreeDTO menuTreeDTO = BeanUtil.copyProperties(menuEntity,MenuTreeDTO.class);
		menuTreeDTO.setAlwaysShow(false);
		MetaDTO metaDTO = new MetaDTO();
		menuTreeDTO.setMeta(metaDTO);
		metaDTO.setIcon(menuTreeDTO.getIcon());
		metaDTO.setTitle(menuTreeDTO.getName());
		metaDTO.setNoCache(true);
		return menuTreeDTO;
	}
}
